/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.base;

import android.content.Intent;
import android.os.Bundle;

import com.ouertech.android.agm.lib.ui.base.defaults.activity.BaseTopActivity;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.QQAccessToken;
import com.simon.mengkou.data.bean.base.SinaAccessToken;
import com.simon.mengkou.data.bean.base.WeixinAccessToken;
import com.simon.mengkou.data.enums.EAccountType;
import com.simon.mengkou.system.constant.CstOuer;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.utils.UtilOuer;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/3/8.
 * desc    : 社交绑定基类
 */
public abstract class BaseSocialBindActivity extends BaseTopActivity {
    //友盟社交api
    private UMShareAPI mShareAPI;

    /**
     *
     * @param token
     */
    public abstract void onBindSina(SinaAccessToken token);

    /**
     *
     * @param token
     */
    public abstract void onBindQQ(QQAccessToken token);

    /**
     *
     * @param token
     */
    public abstract void onBindWeixin(WeixinAccessToken token);


    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        mShareAPI = UMShareAPI.get(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mShareAPI.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 新浪绑定
     */
    public void bindSina() {
        SHARE_MEDIA platform = SHARE_MEDIA.SINA;

        if(mShareAPI.isAuthorize(this, platform)) {
            SinaAccessToken token = SinaAccessToken.readAccessToken(this);
            if(token != null && token.isSessionValid() && !token.isExpires()) {
                onBindSina(token);
            } else {
                mShareAPI.doOauthVerify(this, platform, umAuthListener);
            }
        } else {
            mShareAPI.doOauthVerify(this, platform, umAuthListener);
        }
    }

    /**
     * QQ绑定
     */
    public void bindQQ() {
        SHARE_MEDIA platform = SHARE_MEDIA.QQ;

        if(mShareAPI.isAuthorize(this, platform)) {
            QQAccessToken token = QQAccessToken.readAccessToken(this);
            if(token != null && token.isSessionValid() && !token.isExpires()) {
                onBindQQ(token);
            } else {
                mShareAPI.doOauthVerify(this, platform, umAuthListener);
            }
        } else {
            mShareAPI.doOauthVerify(this, platform, umAuthListener);
        }
    }

    /**
     * 微信绑定
     */
    public void bindWeixin() {
        SHARE_MEDIA platform = SHARE_MEDIA.WEIXIN;

        if(mShareAPI.isAuthorize(this, platform)) {
            WeixinAccessToken token = WeixinAccessToken.readAccessToken(this);
            if(token != null && token.isSessionValid() && !token.isExpires()) {
                onBindWeixin(token);
            } else {
                mShareAPI.doOauthVerify(this, platform, umAuthListener);
            }
        } else {
            mShareAPI.doOauthVerify(this, platform, umAuthListener);
        }
    }

    /**
     * 授权监听
     */
    private UMAuthListener umAuthListener = new UMAuthListener() {
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
            if(platform == SHARE_MEDIA.SINA) {//新浪
                if(data != null) {
                    SinaAccessToken token = new SinaAccessToken();
                    token.setUid(data.get(SinaAccessToken.KEY_UID));
                    token.setAccessToken(data.get(SinaAccessToken.KEY_ACCESS_TOKEN));
                    token.setRefreshToken(data.get(SinaAccessToken.KEY_REFRESH_TOKEN));
                    token.setExpiresIn(data.get(SinaAccessToken.KEY_EXPIRES_IN));

                    if(token.isSessionValid()) {
                        SinaAccessToken.writeAccessToken(BaseSocialBindActivity.this, token);
                        onBindSina(token);
                    } else {
                        UtilOuer.toast(BaseSocialBindActivity.this, R.string.common_bind_failure);
                    }
                }
            } else if(platform == SHARE_MEDIA.QQ) {//qq
                if(data != null) {
                    QQAccessToken token = new QQAccessToken();
                    token.setUid(data.get(QQAccessToken.KEY_UID));
                    token.setAccessToken(data.get(QQAccessToken.KEY_ACCESS_TOKEN));
                    token.setAppId(CstOuer.TENCENT.TENCENT_APP_ID);
                    token.setExpiresIn(data.get(QQAccessToken.KEY_EXPIRES_IN));
                    token.setOpenId(data.get(QQAccessToken.KEY_OPENID));

                    if(token.isSessionValid()) {
                        QQAccessToken.writeAccessToken(BaseSocialBindActivity.this, token);
                        onBindQQ(token);
                    } else {
                        UtilOuer.toast(BaseSocialBindActivity.this, R.string.common_bind_failure);
                    }
                }
            } else if(platform == SHARE_MEDIA.WEIXIN) {//微信
                if(data != null) {
                    WeixinAccessToken token = new WeixinAccessToken();
                    token.setOpenId(data.get(WeixinAccessToken.KEY_OPENID));
                    token.setAccessToken(data.get(WeixinAccessToken.KEY_ACCESS_TOKEN));
                    token.setRefreshToken(data.get(WeixinAccessToken.KEY_REFRESH_TOKEN));
                    token.setExpiresIn(data.get(WeixinAccessToken.KEY_EXPIRES_IN));
                    token.setUnionId(data.get(WeixinAccessToken.KEY_UNIONID));
                    token.setScope(data.get(WeixinAccessToken.KEY_SCOPE));

                    if(token.isSessionValid()) {
                        WeixinAccessToken.writeAccessToken(BaseSocialBindActivity.this, token);
                        onBindWeixin(token);
                    } else {
                        UtilOuer.toast(BaseSocialBindActivity.this, R.string.common_bind_failure);
                    }
                }
            }
        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            UtilOuer.toast(BaseSocialBindActivity.this, R.string.common_bind_failure);
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {

        }
    };
}
